import 'package:barcode_widget/barcode_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:my_flutter/nativeUtil/NativeUtil.dart';
import 'package:my_flutter/widgets/GoodsLabelHelp.dart';
import 'package:my_flutter/widgets/TextHelp.dart';
import 'package:screenshot/screenshot.dart';

///@ClassName: PrintPage
/// @Author: guoyangyang
///@Date: 2022 3/28 2:15 PM
///@Description: 打印顺丰快递标签

class PrintPage extends StatefulWidget {
  const PrintPage({Key? key}) : super(key: key);

  @override
  State<PrintPage> createState() => _PrintPageState();
}

class _PrintPageState extends State<PrintPage> {
  //截标签的
  ScreenshotController screenshotPrint = ScreenshotController();

  void saveImage() async{
    screenshotPrint
        .capture(delay: Duration(milliseconds: 10))
        .then((capturedImage) async {
      saveUint8ListImage(capturedImage, "biaoqian");
    }).catchError((onError) {
      debugPrint(onError);
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint('PrintPage-----build');

    return Scaffold(
      appBar: AppBar(
        title: const Text("PrintPage"),
      ),
      body: Center(
        child: Screenshot(
            controller: screenshotPrint,
            child: Stack(
              children: [
                // 标签 - 文字；
                Container(
                  color: Colors.grey.shade200,
                  width: maxWidth_printer,
                  height: maxHeight_printer,
                  child: CustomPaint(
                    foregroundPainter: GoodsLabelHelp(),
                  ),
                ),
                //标签 - 横条码
                Positioned(
                  // bottom: 0,
                    left: 10 * 5,
                    //指定left和right：相当于指定了宽。宽必须指定，不然图片无法正确绘制
                    top: 8 * 5,
                    child: UnconstrainedBox(
                      child: BarcodeWidget(
                        barcode: Barcode.code128(),
                        data: "SF1002528889848",
                        drawText: false,
                        width: 50 * 5,
                        height: 10 * 5,
                      ),
                    )),
              ],
            )
        )

      ),
      floatingActionButton: FloatingActionButton(
        onPressed: saveImage,
        tooltip: '保存为图片',
        child: const Text('保存'),
      ),
    );
  }
}
